<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>快捷入口</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layui-form layuimini-form" id="editNotice" hidden="hidden">
	<div class="layui-form-item">
		<label class="layui-form-label">ID</label>
		<div class="layui-input-block">
			<input type="text" id="edit_id" name="edit_id" disabled="disabled" value="" class="layui-input">
		</div>
	</div>
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" id="edit_title" name="edit_title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题"class="layui-input">
        </div>
    </div>
	<div class="layui-form-item">
	    <label class="layui-form-label required">分类</label>
	    <div class="layui-input-block">
	       <select id="edit_interest" name="edit_interest" lay-filter="aihao">
	           <option value=""></option>
	           <option value="0">普通</option>
	           <option value="1">中级</option>
	           <option value="2">紧急</option>
	       </select>
	    </div>
	</div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">公告内容</label>
        <div class="layui-input-block">
            <textarea id="edit_detail" name="edit_detail" class="layui-textarea" lay-verify="required" lay-reqtext="公告内容不能为空" placeholder="请输入公告内容"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="editBtn">确认修改</button>
        </div>
    </div>
</div>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '/HHJT/system/noticePageList.do',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {field: 'id', title: 'ID', sort: true},
                {field: 'type', title: '分类', sort: true},
                {field: 'title', title: '标题', sort: true},
                {field: 'create_time',title: '创建时间', sort: true},
				 {field: 'detail', title: '公告内容'},
				 {field: 'memo', title: '备注'},
				 {field: 'creator', title: '创建人ID'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 20, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
        	data = data.field;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , method: 'post'
                , where: {
                    title: data.title
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加公告',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
            	layer.confirm('真的要批量删除以下行吗', function (index) {
            		layer.close(index);
            		var checkStatus = table.checkStatus('currentTableId')
                    , delData = checkStatus.data
                    , delCount = checkStatus.data.length
                    , delList = [];
            		if(delCount===0){
            			layer.msg('至少要选择一个进行批量删除');
            			return false;
            		}
            		var delStr = '';
	                for(var i = 0;i<delCount;i++){
	                    //delList.push(delData[i].id);
	                	delStr += delData[i].id + ',';
	                }
	                console.log(delList);
	                var delId = JSON.stringify(delStr);
                    $.ajax({
						url:'/HHJT/system/deleteNoticeInId.do',
						type:'POST',
						//contentType: "application/json;charset=utf-8",
						data:{
							delStr:delStr
						},
						dataType:'JSON',
						success:function(res){
							if (res.status == 0) {
				    			layer.msg(res.message,{
				    				icon: 6,
				    				time:1000,
				    				},function(){
										parent.location.reload();
				                    });
							} else {
								layer.close(index);
								layer.msg(res.message);
							}
						}
					});
                });
                
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj);
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = data.id;
            var type = data.type;
            var title = data.title;
            var detail = data.detail;
            var memo = data.memo;
            if(type==='普通'){
            	var interest = 0;
            }else if(type==='中级'){
            	var interest = 1;
            }else if(type==='紧急'){
            	var interest = 2;
            }
            console.log(interest)
            if (obj.event === 'edit') {
            	layer.open({
                	title : '编辑公告',
                	type : 1,
                	area : [ '62%', '80%' ],
                	maxmin : true,
                	shadeClose : true,
                	content : $('#editNotice'),
                	shade: [0.8, '#393D49'],
                	success : function(layero, index) {
                		$("#edit_id").val(id);
                		$("#edit_title").val(title);
                		$("#edit_detail").val(detail);
                	    // 遍历select
                	    $("#edit_interest").each(function() {
                	        // this代表的是<option></option>，对option再进行遍历
                	        $(this).children("option").each(function() {
                	            // 判断需要对那个选项进行回显
                	            if (this.value == interest) {
                	                console.log($(this).text());
                	                // 进行回显
                	                $(this).attr("selected","selected");
                	            }
                	        });
                	    });
                	    form.render('select');
                	}
                });
            	//监听提交
                form.on('submit(editBtn)', function (data) {
					var edata = data.field;
		            var eid = edata.edit_id;
		            var etitle = edata.edit_title;
		            var edetail = edata.edit_detail;
		            if(data.edit_interest==0){
		        		var etype = '普通';
		        	}else if(data.edit_interest==1){
		        		var etype = '中级';
		        	}else{
		        		var etype = '紧急';
		        	}
		            //var ememo = data.memo;
					var jsonRes = JSON.stringify({
						'id':eid,
						'type':etype,
						'title':etitle,
						'detail':edetail,
					});
                	layer.confirm('确认修改', {icon: 3, title:'提示'}, function(index){
                		layer.close(index);
						$.ajax({
							url:'/HHJT/system/editNotice.do',
							type:'POST',
							contentType: "application/json;charset=utf-8",
							data:jsonRes,
							dataType:'JSON',
							success:function(res){
								if (res.status == 0) {
					    			layer.msg(res.message,{
					    				icon: 6,
					    				time:1000,
					    				},function(){
				    					var iframeIndex = parent.layer.getFrameIndex(window.name);
					                    parent.layer.close(iframeIndex);
										parent.location.reload();
					                    });
								} else {
									layer.close(index);
									layer.msg(res.message);
								}
							}
						});
                	});
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                	var did = obj.data.id;
                    layer.close(index);
                    $.ajax({
						url:'/HHJT/system/deleteNotice.do?id='+did,
						type:'GET',
						contentType: "application/json;charset=utf-8",
						dataType:'JSON',
						success:function(res){
							if (res.status == 0) {
				    			layer.msg(res.message,{
				    				icon: 6,
				    				time:1000,
				    				},function(){
				    					obj.del();
				                    });
							} else {
								layer.close(index);
								layer.msg(res.message);
							}
						}
					});
                });
            }
        });
    });
</script>

</body>
</html>